package com.leetcode.LC;

public class LC1652 {
    public int[] decrypt(int[] code, int k) {
        //数组拼接
        int[] ints = new int[code.length + Math.abs(k)];
        if (k > 0) {
            for (int i = 0; i < ints.length; i++) {
                ints[i] = code[i % code.length];
            }
            for (int i = 0; i < code.length; i++) {
                int sum = 0;
                for (int j = i + 1; j <= i + k; j++) {
                    sum += ints[j];
                }
                code[i] = sum;
            }
        } else if (k < 0) {
            for (int i = 0, a = k; i < ints.length; i++, a++) {
                if (code.length + a == code.length) {
                    a = 0;
                    while (a < code.length) {
                        ints[i++] = code[a++];
                    }
                } else {
                    ints[i] = code[code.length + a];
                }
            }
            for (int i = 0; i < code.length; i++) {
                int sum = 0;
                for (int j = i + ( -k)-1; j >= i ; j--) {
                    sum += ints[j];
                }
                code[i] = sum;
            }
        } else if (k == 0) {
            for (int i = 0; i < code.length; i++) {
                code[i] = 0;
            }
        }
        return code;
    }

    public static void main(String[] args) {
        for (int i : new LC1652().decrypt(new int[]{2, 4, 9, 3}, 2)) {
            System.out.print(i+",");
        }
    }
}
